package com.itcast.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.mapper.StudentMapper;
import com.itcast.pojo.*;
import com.itcast.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 学员列表数据的条件分页查询
     * @param param
     * @return
     */
    public PageBean listQuery(StudentQueryParam param) {
        PageHelper.startPage(param.getPage(),param.getPageSize());
        Page<Student> rows = studentMapper.listQuery(param);
        return new PageBean(rows.getTotal(),rows);
    }

    /**
     * 批量删除学员信息
     * @param ids
     * @return
     */
    public void delete(List<Integer> ids) {
        studentMapper.delete(ids);
    }

    /**
     * 添加学员信息
     * @param student
     * @return
     */
    public void add(Student student) {
        studentMapper.add(student);
    }

    /**
     * 根据主键ID查询学员的信息
     * @param id
     * @return
     */
    public Student getById(Integer id) {
        return studentMapper.getById(id);
    }

    /**
     * 修改学员的数据信息
     * @param student
     * @return
     */
    public void update(Student student) {
        studentMapper.update(student);
    }

    /**
     * 修改学员的违纪信息
     * @param id
     * @param score
     * @return
     */
    public void violationUpdate(Integer id, Integer score) {
        studentMapper.violationUpdate(id,score);
    }

    /**
     * 统计学员的学历信息
     * @return
     */
    public List<StudentDegreeData> studentDegreeData() {
        return studentMapper.studentDegreeData();
    }

    /**
     * 统计每一个班级的人数
     * @return
     */
    public StudentCountData studentCountData() {
        List<CountData> countDataList = studentMapper.studentCountData();
        List<String> clazzList = countDataList.stream().map(CountData::getClazzName).collect(Collectors.toList());
        List<Integer> dataList = countDataList.stream().map(CountData::getTotal).collect(Collectors.toList());
        return new StudentCountData(clazzList,dataList);
    }
}
